<?php
class DmdaotaoModelDetainckh extends JModelLegacy {
	function __construct() {
		parent::__construct ();
		
		$array = JRequest::getVar ( 'cid', 0, '', 'array' );
		global $mainframe, $option;
		
		$mainframe = JFactory::getApplication ();
		
		// Get the pagination request variables
		$limit = $mainframe->getUserStateFromRequest ( 'global.list.limit', 'limit', $mainframe->getCfg ( 'list_limit' ), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest ( $option . '.limitstart', 'limitstart', 0, 'int' );
		// In case limit has been changed, adjust limitstart accordingly
		$limitstart = ($limit != 0 ? (floor ( $limitstart / $limit ) * $limit) : 0);
		$this->setState ( 'limit', $limit );
		$this->setState ( 'limitstart', $limitstart );
	}
	function _buildQuery($tb = null, $order = null) {
		$db = JFactory::getDbo ();
		$post = JRequest::get ( 'post' );
		$where = array ();
		$query = 'select * from ' . $tb;
		if (! empty ( $post ['name_search'] )) {
			$where [] = 'name LIKE ' . $db->quote ( '%' . $db->escape ( $post ['name_search'], true ) . '%', false );
		}
		$where = (count ( $where )) ? implode ( ' AND ', $where ) : '';
		if (! empty ( $where )) {
			$query .= ' WHERE ' . $where;
		}
		$query .= ' order by ' . $order;
		return $query;
	}
	function getData() {
    // if data hasn't already been obtained, load it
            if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));    
            }
            return $this->_data;
      }


    function getTotal(){
    // Load the content if it doesn't already exist
        if (empty($this->_total)) {
        $query = $this->_buildQuery();
        $this->_total = $this->_getListCount($query);       
        }
        return $this->_total;
     }

    function getPagination(){
     // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
        jimport('joomla.html.pagination');
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
	function getListFooter(){
		global $mainframe;
 
        $list = array();
        $list['limit']                  = $this->limit;
        $list['limitstart']             = $this->limitstart;
        $list['total']                  = $this->total;
        $list['limitfield']             = $this->getLimitBox();
        $list['pagescounter']   = $this->getPagesCounter();
        $list['pageslinks']             = $this->getPagesLinks();
 
        $chromePath             = JPATH_THEMES.DS.$mainframe->getTemplate().DS.'html'.DS.'pagination.php';
        if (file_exists( $chromePath ))
        {
                require_once( $chromePath );
                if (function_exists( 'pagination_list_footer' )) {
                        return pagination_list_footer( $list );
                }
        }
        return $this->_list_footer($list);
	}
	public function create($formData) {
		// $table = Core::table('Danhmuc/AbilityCode');
		$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
		$src ['name'] = $formData ['name'];
		$src ['status'] = $formData ['status'] == "on" ? 1 : 0;
		$src ['order'] = $formData ['order'];
		return $table->save ( $src );
	}
	public function update($formData) {
		// $table = Core::table('Danhmuc/AbilityCode');
		$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
		// var_dump($formData);exit;
		$src ['id'] = $formData ['id'];
		$src ['name'] = $formData ['name'];
		$src ['status'] = $formData ['status'] == "on" ? 1 : 0;
		$src ['order'] = $formData ['order'];
		return $table->save ( $src );
	}
	public function checkTrungten($name, $id = null) {
		$db = JFactory::getDbo ();
		$query = $db->getQuery ( true );
		$query->select ( 'count(name)' );
		$query->from ( 'dm_detainckh' );
		$where = 'name like "' . $name . '"';
		if ($id > 0)
			$where = 'id !=' . $id; // dùng cho trường hợp edit
		$query->where ( $where );
		
		$db->setQuery ( $query );
		// $this->number = $db->loadResult();
		return $db->loadResult ();
	}
	public function read($id) {
		// $table = Core::table('Danhmuc/AbilityCode');
		$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
		if (! $table->load ( $id )) {			return null;
		}
		$fields = array_keys ( $table->getFields () );
		$data = array ();
		for($i = 0; $i < count ( $fields ); $i ++) {
			$data [$fields [$i]] = $table->$fields [$i];
		}

		return $data;
	}
	public function delete($cid) {
		// $table = Core::table('Danhmuc/AbilityCode');
		$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
		if (! is_array ( $cid ) || count ( $cid ) == 0) {
			$flag = false;
		} else {
			for($i = 0; $i < count ( $cid ); $i ++) {
				$flag = $table->delete ( $cid [$i] );
			}
		}
		return $flag;
	}
	public function findAll($params = null, $order = null, $offset = null, $limit = null) {
		// $table = Core::table('Danhmuc/AbilityCode');
		$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
		// var_dump($table); exit;
		$db = $table->getDbo ();
		$query = $db->getQuery ( true );
		$query->select ( array (
				'*' 
		) )->from ( $table->getTableName () );
		
		if (isset ( $params ['name'] ) && ! empty ( $params ['name'] )) {
			$query->where ( 'name LIKE (' . $db->quote ( '%' . $params ['name'] . '%' ) . ')' );
		}
		if ($order == null) {
			$query->order ( 'id' );
		} else {
			$query->order ( $order );
		}
		
		if ($offset != null && $limit != null) {
			$db->setQuery ( $query, $offset, $limit );
		} else {
			$db->setQuery ( $query );
		}
		return $db->loadObjectList ();
	}
	function publish($cid = array(), $publish = 1) {
		$flag = false;
		if (count ( $cid )) {
			JArrayHelper::toInteger ( $cid );
			// $table = Core::table('Danhmuc/AbilityCode');
			$table = JTable::getInstance ( 'DmDetainckh', 'Table' );
			$src ['status'] = $publish;
			for($i = 0; $i < count ( $cid ); $i ++) {
				$src ['id'] = $cid [$i];
				$flag = $table->save ( $src );
			}
		}
		return $flag;
	}
}